package com.example.springboot004mybatis.mapper;

import com.example.springboot004mybatis.model.UserInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface UserInfoMapper {
    
    /**
     * @Description: 查所有
     * @auther: uu
     * @date: 2023/6/16 18:05
     */
    @Select("""
            select * from user_info
            """)
    @ResultMap("userInfo")
    List<UserInfo> selectAll();

    // 一对一查询

    /**
     * @Description: 根据id查询用户的信息, 一对多
     * @auther: uu
     * @date: 2023/6/16 1:43
     */
    @Select("""
            select * from user_info 
            where id = #{id}
            order by id;
            """)
    @Results(id = "userInfo", value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "age", property = "age"),
            @Result(column = "sex", property = "sex"),
            @Result(column = "id", property = "userImpressions",
            many = @Many(select = "com.example.springboot004mybatis.mapper.UserImpressionMapper.selectByUserId",
                    fetchType = FetchType.LAZY))
    })
    UserInfo selectById(Integer id);
    


    /**
     * @Description: 增加用户信息
     * @auther: uu
     * @date: 2023/6/16 17:56
     */

    @Insert("""
            insert into user_info values(#{id},#{name},#{age},#{sex});
            """)
    @ResultMap("userInfo")
    Integer insertUser(UserInfo userInfo);

    /**
     * @Description: 根据用户id删除
     * @auther: uu
     * @date: 2023/6/16 17:59
     */
    @Delete("""
            delete from user_info where id = #{id}
            """)
    @ResultMap("userInfo")
    Integer delById(Integer id);


    /**
     * @Description: 更新用户信息
     * @auther: uu
     * @date: 2023/6/16 18:04
     */
    @Update("""
            update user_info set name = #{name}, name = #{name}, 
            age = #{age}, sex = #{sex}
            where id = #{id}
            """)
    @ResultMap("userInfo")
    Integer updateUserInfo(UserInfo userInfo);


}
